﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SolidWorks.Interop.sldworks;

namespace SolidworksDLL
{
    public class ModelDocHelp
    {
        public static string SaveDoc(string NewFilePath, object ModelDoc = null)
        {
            if (ModelDoc == null)
            {
                ModelDoc = SolidWorksHelp.swApp.ActiveDoc;
            }

            ModelDoc2 swModel = (ModelDoc2)ModelDoc;
            ModelDocExtension modelExt = swModel.Extension;
            return SaveDoc(modelExt, NewFilePath);
        }
        /// <summary>
        /// 保存文件
        /// </summary>
        /// <param name="modelExt"></param>
        /// <param name="NewFilePath">保存路径</param>
        /// <returns>成功返回空字符串，失败返回错误信息</returns>
        public static string SaveDoc(ModelDocExtension modelExt, string NewFilePath)
        {
            int err = 0;
            int war = 0;
            bool b = modelExt.SaveAs(NewFilePath, 0, 1, null, ref err, ref war);

            if (b == false || err != 0)
            {
                return "保存文档到：" + NewFilePath + "出现错误，错误代号：" + err.ToString();
            }
            return "";
        }

        /// <summary>
        /// 打开指定路径的SW文档
        /// </summary>
        /// <param name="FilePath">文件路径</param>
        /// <returns>成功返回空字符串，失败返回错误信息</returns>
        public static string OpenDoc(string FilePath)
        {
            string ext = Path.GetExtension(FilePath).ToUpper();
            string FileName = Path.GetFileName(FilePath);

            int type = 0;
            switch (ext)
            {
                case ".SLDPRT":
                    type = 1;
                    break;
                case ".SLDASM":
                    type = 2;
                    break;
                case ".SLDDRW":
                    type = 3;
                    break;
                case ".X_T":
                    type = 4;
                    break;
                case ".STP":
                    type = 5;
                    break;
                case ".STEP":
                    type = 6;
                    break;                   
                case ".igs":
                    type = 7;
                    break;
                default:
                    break;
            }
            if (type == 0)
            {
                return "不是零件、装配体、3D格式、工程图,";
            }



            int err = 0;
            int war = 0;

            ModelDoc2 swModel = SolidWorksHelp.swApp.ActivateDoc(FileName);      //
            if (swModel != null)
            {
                return "当前已打开文件：" + swModel.GetPathName();

            }

            if (type == 4)
            {
                string FilePathName = Path.GetDirectoryName(FilePath);
                string Name = Path.GetFileNameWithoutExtension(FilePath);
                swModel = SolidWorksHelp.swApp.LoadFile4(FilePath, "r", null, ref err);
                SaveDoc(FilePathName + "\\" + Name + ".SLDPRT");
            }
            else
            {
                swModel = SolidWorksHelp.swApp.OpenDoc6(FilePath, type, 1, "", ref err, ref war);       //LoadFile4打开其他格式文档
            }
            if (type == 2)
            {              
                AssemblyDoc swModel2 = (AssemblyDoc)swModel;
                swModel2.ResolveAllLightWeightComponents(false);
            }

            if (swModel == null)
            {
                return "打开文件：" + FilePath + "失败，错误代号：" + err.ToString();
            }
            else if (err != 0)
            {
                return "打开文件：" + FilePath + "错误，错误代号：" + err.ToString();
            }
            else
            {
                return "";
            }

        }
        /// <summary>
        /// 获取打开的所有文件路径（不包括轻化的零件）
        /// </summary>
        /// <returns>返回一个列表</returns>
        public static List<string> GetAllDoc()
        {
            if (SolidWorksHelp.swApp == null)
            {
                MessageBox.Show("请先连接到solidworks对象");
                return null;

            }
            List<string> modelPaths = new List<string>();

            int count = SolidWorksHelp.swApp.GetDocumentCount();
            if (count == 0)
            {
                return modelPaths;
            }
            var swModels = (object[])SolidWorksHelp.swApp.GetDocuments();

            foreach (ModelDoc2 item in swModels)
            {
                modelPaths.Add(item.GetPathName());
            }

            return modelPaths;

        }
       
    }
}
